package com.douxg.fastCoding.db;

import cn.hutool.core.util.StrUtil;
import com.jfinal.kit.StrKit;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/5 17:21
 */
@Data
@Accessors(chain = true)
public class ConfigEO extends Config {

    public String getEntityName() {
        return StrUtil.upperFirst(getEntityVarName());
    }

    public String getEntityVarName() {
        String tableName = getTableName();
        if (tableName == null) {
            return null;
        }
        tableName = tableName.toLowerCase();
        String tablePrefix = getTablePrefix();
        if (tablePrefix != null) {
            tableName = tableName.replace(tablePrefix.toLowerCase(), "");
        }
        String tableSuffix = getTableSuffix();
        if (tableSuffix != null) {
            tableName = tableName.replace(tableSuffix.toLowerCase(), "");
        }
        if (tableName.startsWith("_")) {
            tableName = tableName.substring(1);
        }
        if (tableName.endsWith("_")) {
            tableName = tableName.substring(0, tableName.length() - 1);
        }
        return StrKit.toCamelCase(tableName, true);
    }

    public String getDtoName() {
        return nameSuffixJoin(getDtoSuffix());
    }

    public String getDtoVarName() {
        return StrUtil.lowerFirst(getDtoName());
    }

    public String getVoName() {
        return nameSuffixJoin(getVoSuffix());
    }

    public String getVoVarName() {
        return StrUtil.lowerFirst(getVoName());
    }

    public String getMapperName() {
        return nameSuffixJoin(getMapperSuffix());
    }

    public String getMapperVarName() {
        return StrUtil.lowerFirst(getMapperName());
    }

    public String getServiceName() {
        return nameSuffixJoin(getServiceSuffix());
    }

    public String getServiceVarName() {
        return StrUtil.lowerFirst(getServiceName());
    }

    public String getServiceImplName() {
        return nameSuffixJoin(getServiceImplSuffix());
    }

    public String getServiceImplVarName() {
        return StrUtil.lowerFirst(getServiceImplName());
    }

    public String getControllerName() {
        return nameSuffixJoin(getControllerSuffix());
    }

    public String getAllEntityName() {
        return join(getAllEntityPackage(), getEntityName());
    }

    public String getAllDtoName() {
        return join(getAllDtoPackage(), getDtoName());
    }

    public String getAllVoName() {
        return join(getAllVoPackage(), getVoName());
    }

    public String getAllMapperName() {
        return join(getAllMapperPackage(), getMapperName());
    }

    public String getAllServiceName() {
        return join(getAllServicePackage(), getServiceName());
    }

    public String getAllServiceImplName() {
        return join(getAllServiceImplPackage(), getServiceImplName());
    }

    public String getAllControllerName() {
        return join(getAllControllerPackage(), getControllerName());
    }

    public String getAllResultName() {
        return join(getAllResultPackage(), getResultName());
    }

    public String getAllEntityPackage() {
        return packageJoin(getEntityPackage());
    }

    public String getAllDtoPackage() {
        return packageJoin(getDtoPackage());
    }

    public String getAllVoPackage() {
        return packageJoin(getVoPackage());
    }

    public String getAllMapperPackage() {
        return packageJoin(getMapperPackage());
    }

    public String getAllServicePackage() {
        return packageJoin(getServicePackage());
    }

    public String getAllServiceImplPackage() {
        return packageJoin(getServiceImplPackage());
    }

    public String getAllControllerPackage() {
        return packageJoin(getControllerPackage());
    }

    public String getAllResultPackage() {
        return packageJoin(getResultPackage());
    }

    private String join(String pack, String name) {
        if (name == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        if (pack != null) {
            sb.append(pack).append(".");
        }
        sb.append(name);
        return sb.toString();
    }

    private String packageJoin(String pack) {
        if (pack == null) {
            return null;
        }
        if (pack.startsWith("~") || pack.startsWith("@")) {
            return pack.substring(1);
        }
        StringBuilder sb = new StringBuilder();
        String projectPackage = getProjectPackage();
        if (projectPackage == null) {
            projectPackage = "";
        }
        Map<String, String> map = new HashMap<>();
        map.put("pack", pack);
        map.put("projectPackage", projectPackage);
        Map<String, String> result = handlePack(map);
        pack = result.get("pack");
        projectPackage = result.get("projectPackage");
        sb.append(projectPackage).append(".").append(pack);
        return sb.toString();
    }

    private Map<String, String> handlePack(Map<String, String> map) {
        String pack = map.get("pack");
        if (pack.startsWith(".")) {
            Map<String, String> result = new HashMap<>();
            String projectPackage = map.get("projectPackage");
            int index = projectPackage.lastIndexOf(".");
            if (index >= 0) {
                projectPackage = projectPackage.substring(0, index);
            }
            pack = pack.substring(1);
            result.put("pack", pack);
            result.put("projectPackage", projectPackage);
            return handlePack(result);
        }
        return map;
    }

    private String nameSuffixJoin(String suffix) {
        String entityName = getEntityName();
        if (entityName != null && suffix != null) {
            return entityName.concat(suffix);
        }
        return null;
    }

    public String getPageQueryOrderByFmt() {
        String pageQueryOrderBy = getPageQueryOrderBy();
        if (pageQueryOrderBy == null) {
            return null;
        }
        return getMapperName() + "." + pageQueryOrderBy.toUpperCase();
    }

    public boolean isTimeRoundQueryColumn(String column) {
        List<String> timeRoundQueryColumn = getTimeRoundQueryColumn();
        return timeRoundQueryColumn != null && timeRoundQueryColumn.size() > 0 && timeRoundQueryColumn.contains(column);
    }
}
